home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / AOCE / Development Tools / Sample Code / Digital Signatures / Digital Signature Demo / Source ƒ / DemoSignedObject.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-11  |  5.2 KB  |  258 lines  |  [TEXT/KAHL]

  1. /*
  2.  * DemoSignedObject.c
  3.  *    Copyright © 1992-93 Apple Computer Inc. All rights reserved.
  4.  * This sub-class to CSignedObject demonstrates how the Digital
  5.  * Signature Manager may be used to sign and verify individual
  6.  * data objects.
  7.  */
  8. #include "Demo.h"
  9. #include "DemoSignedObject.h"
  10. #include <CDataFile.h>
  11. #include <Global.h>
  12. #include <Exceptions.h>
  13. #include <CError.h>
  14. #include <TBUtilities.h>
  15. #include <OCEErrors.h>
  16. extern CError                *gError;
  17. extern CursHandle            gWatchCursor;
  18. extern OSType                gSignature;
  19.  
  20. void                        Message(
  21.         const StringPtr            textString
  22.     );
  23.  
  24.  
  25. void
  26. DemoSignedObject::IDemoSignedObject(void)
  27. {
  28.         inherited::ISignedObject();
  29.         itsCheckBoxValue = kExquisiteMask;
  30.         itsRadioButtonValue = 1;
  31.         CopyPString("\pIt’s actually very nice.", itsCommentaryText);
  32.         itsSignatureDataFile = NULL;
  33. }
  34.  
  35. unsigned short
  36. DemoSignedObject::GetCheckBoxValues(void)
  37. {
  38.         return (itsCheckBoxValue);
  39. }
  40.  
  41. void
  42. DemoSignedObject::SetCheckBoxValues(
  43.     unsigned short            newCheckBoxValues
  44. )
  45. {
  46.         itsCheckBoxValue = newCheckBoxValues;
  47. }
  48.  
  49. unsigned short
  50. DemoSignedObject::GetRadioButtonValue(void)
  51. {
  52.         return (itsRadioButtonValue);
  53. }
  54.  
  55. void
  56. DemoSignedObject::SetRadioButtonValue(
  57.     unsigned short            newRadioButtonValue
  58. )
  59. {
  60.         itsRadioButtonValue = newRadioButtonValue;
  61. }
  62.  
  63. void
  64. DemoSignedObject::GetCommentaryText(
  65.     StringPtr                resultText
  66. )
  67. {
  68.         CopyPString(itsCommentaryText, resultText);
  69. }
  70.  
  71. void
  72. DemoSignedObject::SetCommentaryText(
  73.     const StringPtr            newText
  74. )
  75. {
  76.         CopyPString(newText, itsCommentaryText);
  77. }
  78.  
  79. /*
  80.  * Sign away. We keep the signature context around
  81.  * for the entire modal dialog operation.
  82.  */
  83. Boolean
  84. DemoSignedObject::SignThisObject(void)
  85. {
  86.         Boolean            signSuccessful;
  87.         
  88.         signSuccessful = FALSE;
  89.         TRY {
  90.             SignPrepare(NULL, "\p");
  91.             ProcessObject();
  92.             Sign(gSIGStatusProc);
  93.             signSuccessful = TRUE;
  94.         }
  95.         CATCH {
  96.             NO_PROPAGATE;
  97.             switch (gLastError) {
  98.             case userCanceledErr:
  99.                 Message("\pUser cancelled signing");
  100.                 break;
  101.             case kSIGPasswordErr:
  102.                 Message("\pPassword incorrect");
  103.                 break;
  104.             case kSIGSignerErr:
  105.                 Message("\pSigner error");
  106.                 break;
  107.             case kSIGSignerNotValidErr:
  108.                 Message("\pSigner invalid or expired");
  109.                 break;
  110.             default:
  111.                 gError->CheckOSError(gLastError);
  112.                 break;
  113.             }
  114.         }
  115.         ENDTRY;
  116.         /*
  117.          * If we succeed, there's a signature in itsSignature
  118.          * and a pending context.
  119.          */
  120.         return (signSuccessful);
  121. }
  122.  
  123. Boolean
  124. DemoSignedObject::VerifyThisObject(void)
  125. {
  126.         Boolean                verifySuccessful;
  127.         
  128.         verifySuccessful = FALSE;
  129.         TRY {
  130.             CheckForSignature();
  131.             VerifyPrepare(gSIGStatusProc);
  132.             ProcessObject();
  133.             Verify();
  134.             verifySuccessful = TRUE;
  135.         }
  136.         CATCH {
  137.             NO_PROPAGATE;
  138.             switch (gLastError) {
  139.             case kSIGNoSignature:
  140.                 Message(
  141.                     "\pYou must create or read"
  142.                     " a signature first."
  143.                 );
  144.                 break;
  145.             case userCanceledErr:
  146.                 Message("\pUser cancelled verification");
  147.                 break;
  148.             case kSIGSignerErr:
  149.                 Message("\pThe signature is not valid");
  150.                 break;
  151.             case kSIGVerifyFailedErr:
  152.                 Message("\pVerify failed");
  153.                 break;
  154.             case kSIGInvalidCredentialErr:
  155.                 Message(
  156.                     "\pVerify succeeded, but credentials"
  157.                     " are out of date or unverified."
  158.                 );
  159.                 verifySuccessful = TRUE;
  160.                 break;
  161.             default:
  162.                 gError->CheckOSError(gLastError);
  163.                 break;
  164.             }
  165.         }
  166.         ENDTRY;
  167.         if (verifySuccessful)
  168.             ShowSigner("\p");
  169.         /*
  170.          * We don't dispose of the context so that the
  171.          * user can choose "Show Signer."
  172.          */
  173.         return (verifySuccessful);
  174. }
  175.  
  176. void
  177. DemoSignedObject::ProcessObject(void)
  178. {
  179.         /*
  180.          * Note: ProcessData does not move or purge memory,
  181.          * so we don't need to lock down the object.
  182.          */
  183.         ProcessData(&itsCheckBoxValue, sizeof itsCheckBoxValue);
  184.         ProcessData(&itsRadioButtonValue, sizeof itsRadioButtonValue);
  185.         ProcessData(&itsCommentaryText[1], itsCommentaryText[0]);
  186. }
  187.  
  188. /*
  189.  * This is a straight-forward method that writes the signature
  190.  * to a file. In a real-world situation, you would typically
  191.  * also write the dialog (or document) contents.
  192.  */
  193. void
  194. DemoSignedObject::SaveObjectSignature(void)
  195. {
  196.         CDataFile                *aDataFile;
  197.         SFReply                    macSFReply;
  198.         Point                    corner;
  199.         
  200.         CheckForSignature();
  201.         FindDlogPosition('DLOG', putDlgID, &corner);
  202.         SFPutFile(
  203.             corner,
  204.             "\pSave Signature as:",
  205.             "\pUntitled Signature",
  206.             NULL,
  207.             &macSFReply
  208.         );
  209.         if (macSFReply.good) {
  210.             aDataFile = new (CDataFile);
  211.             TRY {
  212.                 aDataFile->IDataFile();
  213.                 aDataFile->SFSpecify(&macSFReply);
  214.                 aDataFile->CreateNew(gSignature, kSignatureFileType);
  215.                 aDataFile->Open(fsRdWrPerm);
  216.                 WriteSignature(aDataFile);
  217.                 aDataFile->Close();
  218.             }
  219.             CATCH {
  220.                 ForgetObject(aDataFile);
  221.             }
  222.             ENDTRY;
  223.             ForgetObject(aDataFile);
  224.         }
  225. }
  226.  
  227. /*
  228.  * This method reads a signature from a file created by
  229.  * SaveSignature.
  230.  */
  231. void
  232. DemoSignedObject::ReadObjectSignature(void)
  233. {
  234.         CDataFile                *aDataFile;
  235.         SFReply                    macSFReply;    
  236.         Point                    corner;
  237.         SFTypeList                sigTypeList;
  238.         
  239.         sigTypeList[0] = kSignatureFileType;
  240.         FindDlogPosition('DLOG', getDlgID, &corner);
  241.         SFGetFile(corner, NULL, NULL, 1, sigTypeList, NULL, &macSFReply);
  242.         if (macSFReply.good) {        
  243.             aDataFile = new (CDataFile);
  244.             TRY {
  245.                 aDataFile->IDataFile();
  246.                 aDataFile->SFSpecify(&macSFReply);
  247.                 aDataFile->Open(fsRdWrPerm);
  248.                 ReadSignature(aDataFile);
  249.                 aDataFile->Close();
  250.             }
  251.             CATCH {
  252.                 ForgetObject(aDataFile);
  253.             }
  254.             ENDTRY;
  255.             ForgetObject(aDataFile);
  256.         }
  257. }
  258.